package com.quectel.emp.controller.household;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdFamilyDto;
import com.quectel.core.module.household.service.HouseholdFamilyService;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.emp.controller.BaseController;
import com.quectel.emp.util.SessionHolder;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/03/10 11:46
 */
@RestController
@RequestMapping("householdFamily")
@Tag(name = "HouseholdFamilyController", description = "住户家庭相关api")
public class HouseholdFamilyController extends BaseController {
    @DubboReference
    private HouseholdService householdService;
    @DubboReference
    private HouseholdFamilyService householdFamilyService;


    /**
     * 根据id查询住户家庭信息，编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @Operation(summary = "根据id查询住户家庭信息，编辑查询反显")
    public Response<HouseholdFamilyDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdFamilyService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdFamilyDto householdFamilyDto = householdFamilyService.selectById(id);

        return Response.<HouseholdFamilyDto>ok().wrap(householdFamilyDto);
    }

    /**
     * 根据id查询住户家庭信息
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @Operation(summary = "根据id查询住户家庭信息")
    public Response<HouseholdFamilyDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdFamilyService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdFamilyDto householdFamilyDto = householdFamilyService.selectById(id);

        return Response.<HouseholdFamilyDto>ok().wrap(householdFamilyDto);
    }

    /**
     * 保存住户家庭信息记录
     *
     * @param householdFamilyDto
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "保存住户家庭信息记录")
    public Response<Object> save(@RequestBody @Validated HouseholdFamilyDto householdFamilyDto) {
        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(LambdaUtil.getFieldName(HouseholdDto::getId), householdFamilyDto.getHouseholdId());
        if (householdService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        householdFamilyDto.setTenantId(SessionHolder.getEmp().getTenantId());
        householdFamilyDto.setVillageId(SessionHolder.getEmp().getVillageId());

        householdFamilyService.save(householdFamilyDto);

        return Response.ok();
    }


    /**
     * 修改住户家庭单条记录
     *
     * @param householdFamilyDto
     * @return
     */
    @PostMapping("update")
    @Operation(summary = "修改住户家庭单条记录")
    public Response<Object> update(@RequestBody @Validated HouseholdFamilyDto householdFamilyDto) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getVillageId), SessionHolder.getEmp().getVillageId());
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(householdFamilyDto.getId()));
        if (householdFamilyService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        householdFamilyDto.setTenantId(SessionHolder.getEmp().getTenantId());
        householdFamilyDto.setVillageId(SessionHolder.getEmp().getVillageId());

        householdFamilyService.updateById(householdFamilyDto);

        return Response.ok();
    }

    /**
     * 批量删除住户家庭记录
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @Operation(summary = "批量删除住户家庭记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        //添加租户和小区筛选
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getTenantId), SessionHolder.getEmp().getTenantId());
        params.put(LambdaUtil.getFieldName(HouseholdFamilyDto::getVillageId), SessionHolder.getEmp().getVillageId());

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (householdFamilyService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        householdFamilyService.deleteBatch(ids);

        return Response.ok();
    }
}
